# -*- coding: utf-8 -*-
import scrapy
from bookstack.items import BookstackItem


class BookSpider(scrapy.Spider):
    name = 'book'
    allowed_domains = ['localhost']
    start_urls = ['http://localhost/books/python面试宝典']
    # 阻塞
    # def start_requests(self):
    #     reqs = []
    #     for url in self.start_urls:
    #         req = scrapy.Request(url,callback=self.parse)
    #         reqs.append(req)
    #     return reqs
    # 非阻塞
    def start_requests(self):
        for url in self.start_urls:
            yield scrapy.Request(url,callback=self.parse_page)


    def parse_page(self, response):
        entity_list = response.xpath(
            '//div[@class="entity-list book-contents"]/a')
        for entity in entity_list:
            # item = {}
            item = BookstackItem()
            href = entity.xpath('.//@href').get()
            text = entity.xpath('.//h4/text()').get()
            item['href'] = href
            item['text'] = text
            yield item

